/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLO;

import DAO.UserDAO;
import DTO.User;
import DTO.Users;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import util.SupportUtil;
import util.TransformerUtil;

/**
 *
 * @author ThanhTai
 */
public class UserBLO {

    public void getAllUserNotInProject(String xmlFilePath, Integer projectID) {
        try {
            UserDAO userDAO = new UserDAO();
            String condition = " Where (username not in (select username from Members Where ProjectID = " + projectID.toString()
                    + ")) and userRole = 'staff'";
            Users users = userDAO.getAllUser(condition);
            SupportUtil util = new SupportUtil();
            util.marshal(xmlFilePath, users);

//            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//            DocumentBuilder db = dbf.newDocumentBuilder();
//            Document doc = db.parse(new File(xmlFilePath));
//            Node usersNode = (doc.getElementsByTagName("users")).item(0);
//
//            //Delete old child
//            NodeList children = usersNode.getChildNodes();
//            for (int i = 0; i < children.getLength(); i++) {
//                if (children.item(i).getNodeName().equals("user")) {
//                    usersNode.removeChild(children.item(i));
//                }
//            }
//
//            //Write data into xml file
//            for (int i = 0; i < users.size(); i++) {
//                User user = users.get(i);
//                //Create element
//                Element userElement = doc.createElement("user");
//                Element usernameElement = doc.createElement("username");
//                Element passwordElement = doc.createElement("password");
//                Element fullnameElement = doc.createElement("fullname");
//                Element userRoleElement = doc.createElement("userRole");
//
//                //Append text for element
//                usernameElement.appendChild(doc.createTextNode(user.getUsername() + ""));
//                passwordElement.appendChild(doc.createTextNode(user.getPassword() + ""));
//                fullnameElement.appendChild(doc.createTextNode(user.getFullName() + ""));
//                userRoleElement.appendChild(doc.createTextNode(user.getUserRole() + ""));
//
//                //Append child
//                userElement.appendChild(usernameElement);
//                userElement.appendChild(passwordElement);
//                userElement.appendChild(fullnameElement);
//                userElement.appendChild(userRoleElement);
//
//                //Append child
//                usersNode.appendChild(userElement);
//                TransformerUtil.writeXML(doc, xmlFilePath);
//            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void getAllUser(String xmlFilePath) {
        try {
            UserDAO userDAO = new UserDAO();
            Users users = userDAO.getAllUser(null);
            SupportUtil util = new SupportUtil();
            util.marshal(xmlFilePath, users);
//            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//            DocumentBuilder db = dbf.newDocumentBuilder();
//            Document doc = db.parse(new File(xmlFilePath));
//            Node usersNode = (doc.getElementsByTagName("users")).item(0);
//
//            //Delete old child
//            NodeList children = usersNode.getChildNodes();
//            for (int i = 0; i < children.getLength(); i++) {
//                if (children.item(i).getNodeName().equals("user")) {
//                    usersNode.removeChild(children.item(i));
//                }
//            }
//
//            //Write data into xml file
//            for (int i = 0; i < users.size(); i++) {
//                User user = users.get(i);
//                //Create element
//                Element userElement = doc.createElement("user");
//                Element usernameElement = doc.createElement("username");
//                Element passwordElement = doc.createElement("password");
//                Element fullnameElement = doc.createElement("fullName");
//                Element userRoleElement = doc.createElement("userRole");
//
//                //Append text for element
//                usernameElement.appendChild(doc.createTextNode(user.getUsername() + ""));
//                passwordElement.appendChild(doc.createTextNode(user.getPassword() + ""));
//                fullnameElement.appendChild(doc.createTextNode(user.getFullName() + ""));
//                userRoleElement.appendChild(doc.createTextNode(user.getUserRole() + ""));
//
//                //Append child
//                userElement.appendChild(usernameElement);
//                userElement.appendChild(passwordElement);
//                userElement.appendChild(fullnameElement);
//                userElement.appendChild(userRoleElement);
//
//                //Append child
//                usersNode.appendChild(userElement);
//                TransformerUtil.writeXML(doc, xmlFilePath);
//            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public DTO.User getUserByUserName(String username) {
        try {
            UserDAO userDao = new UserDAO();
            String condition = " WHERE username = '" + username + "'";
            Users list = userDao.getAllUser(condition);
            if (list != null && list.size() > 0) {
                return list.get(0);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public User getUserForLogin(String username, String password) {
        try {
            UserDAO userDao = new UserDAO();
            String condition = " WHERE username = '" + username + "' and password = '" + password +"'";
            Users list = userDao.getAllUser(condition);
            if (list != null && list.size() > 0) {
                return list.get(0);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
